package com.appkubes.mall.restful.application.system.basic.controller;

import com.appkubes.cloud.config.params.paramValid.ParamValid;
import com.appkubes.common.Response;
import com.appkubes.mall.restful.application.common.base.BaseController;
import com.appkubes.mall.restful.application.system.basic.entity.dto.AppUpdateDTO;
import com.appkubes.mall.restful.application.system.basic.entity.vo.AppVersionVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@Api(tags = "系统更新")
@RestController
@RequestMapping("/system")
public class SystemController extends BaseController {

    @Value("${app.version}")
    private int version;
    @Value("${app.versionName}")
    private String versionName;
    @Value("${app.isForce}")
    private boolean isForce;
    @Value("${app.type}")
    private String type;
    @Value("${app.content}")
    private String content;
    @Value("${app.ios}")
    private String ios;
    @Value("${app.android}")
    private String android;
    @Value("${app.downloadUrl}")
    private String downloadUrl;


    @ApiOperation("app版本更新")
    @GetMapping("/app")
    public Response app(AppVersionVO appVersionVO) {
        AppUpdateDTO appUpdateDTO = new AppUpdateDTO();
        appVersionVO.setPlatform(appVersionVO.getPlatform().toLowerCase());
        appVersionVO.setVersion(appVersionVO.getVersion().replaceAll("\\.", ""));
        Integer versionInt = Integer.valueOf(appVersionVO.getVersion());
        if (versionInt >= version) {
            return Response.error("版本不需要更新");
        }
        appUpdateDTO.setContent(content);
        appUpdateDTO.setVersionName(versionName);
        appUpdateDTO.setVersionCode(version);
        appUpdateDTO.setType(type);
        if ("ALL".equals(type)) {
            appUpdateDTO.setUrl(downloadUrl);
            appUpdateDTO.setIsForce(isForce?1:0);
        } else {
            appUpdateDTO.setIsForce(isForce?1:0);
            if ("android".equals(appVersionVO.getPlatform())) {
                appUpdateDTO.setUrl(android);
            }
            if ("ios".equals(appVersionVO.getPlatform())) {
                appUpdateDTO.setUrl(ios);
            }
        }
        return Response.success(appUpdateDTO);
    }
}
